<?php
session_start();
//包含头
include '../PHP/head.php';
?>
<?php
//包含bootstrap的顶端按钮
include '../PHP/menu.php';
?>


<script type="text/javascript">
//需要一个异步删除记录的函数

	$(document).ready(function() {
    $('button.deletedata').on('click', function() {
					var requestData = {code: $(this).attr("id"), key: "id", table: "deviation"};
                    $('#notes').load('del_data.php', requestData);
                    $(this).parent().parent().hide();
					});

			});
</script>

<br />
<br />
<p class="text-center lead">报价偏离表</p>
<?php
//接收project_id后增加偏离项
if(isset($_GET['project_id'])) {
	//查询编号信息
	require_once ('../FNS/db_fns.php');
	
	$project_id = $_GET['project_id'];
	$subject = $_GET['subject'];
	
	//数据过滤和处理

	if (! get_magic_quotes_gpc()) {
	$project_id=addslashes ($project_id);
	}
	
	$db = db_connect();
	$query="select projects.code as project_code, projects.name as project_name, projects.sale as sale_id, projects.entrust_date, users.name as sale_name from projects inner join users on projects.sale = users.id where projects.id = '".$project_id."'";
	
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//拿出数据
	$row=$result->fetch_assoc ();
	$project_code = stripslashes ($row['project_code']);
	$project_name = stripslashes ($row['project_name']);
	$sale_name = stripslashes ($row['sale_name']);
	$sale_id = stripslashes ($row['sale_id']);

	
	$result->free();
	$db->close();
	
	//列出编号基本信息
	echo "
		<br />
		<div class='row'>
			<div class='col-md-2'><p class='text-right'>报价编号：</p></div>
			<div class='col-md-2'><p>".$project_code."</p></div>
			<div class='col-md-2'><p class='text-right'>项目名称：</p></div>
			<div class='col-md-4'><p>".$project_name."</p></div>
			<div class='col-md-1'><p class='text-right'>销售：</p></div>
			<div class='col-md-1'><p>".$sale_name."</p></div>
		</div>
		<br />
	
		<br />
		<form class='form-horizontal' action = '../PHP/deviation.php' method = 'post'>
		<input type='hidden' name = 'project_id' value = '".$project_id."'>
		<input type='hidden' name = 'subject' value = '".$subject."'>
		
	";
}	
?>

<?php
	
	
if (isset($_POST['project_id'])) {
		
	require_once ('../FNS/db_fns.php');
	
	$project_id = trim($_POST['project_id']);
	$subject = trim($_POST['subject']);
	$ref = trim($_POST['ref']);
	$des = trim($_POST['des']);
			
	//数据过滤和处理
	//加引号避免输入数据库出错
	if (! get_magic_quotes_gpc()) {	
	$project_id = addslashes ($project_id);
	$subject = addslashes ($subject);
	$ref = addslashes ($ref);
	$des = addslashes ($des);
	}
	
	$issue_date = date('Y-m-d');
	
	//连接数据库并插入数据
	//没有判断是否成功，原意图为：如果接下来的列表更新了，说明成功了。
	$db = db_connect();
	
	$query="insert into deviation (`project_id`, `user_id`, `subject`, `ref`, `des`, `issue_date`, `status`) value ('".$project_id."', '".$user_id."', '".$subject."', '".$ref."', '".$des."', '".$issue_date."', '提交')";
	$result=$db->query ($query);
	
	$query="select projects.code as project_code, projects.name as project_name, projects.sale as sale_id, projects.entrust_date, users.name as sale_name from projects inner join users on projects.sale = users.id where projects.id = '".$project_id."'";
	
	$result=$db->query ($query);
	$num_result=$result->num_rows;
	
	//拿出数据
	$row=$result->fetch_assoc ();
	$project_code = stripslashes ($row['project_code']);
	$project_name = stripslashes ($row['project_name']);
	$sale_name = stripslashes ($row['sale_name']);
	$sale_id = stripslashes ($row['sale_id']);

	
	$result->free();
	$db->close();
	
	//列出编号基本信息
	echo "
		<br />
		<div class='row'>
			<div class='col-md-2'><p class='text-right'>报价编号：</p></div>
			<div class='col-md-2'><p>".$project_code."</p></div>
			<div class='col-md-2'><p class='text-right'>项目名称：</p></div>
			<div class='col-md-4'><p>".$project_name."</p></div>
			<div class='col-md-1'><p class='text-right'>销售：</p></div>
			<div class='col-md-1'><p>".$sale_name."</p></div>
		</div>
		<br />
	
		<br />
		<form class='form-horizontal' action = '../PHP/deviation.php' method = 'post'>
		<input type='hidden' name = 'project_id' value = '".$project_id."'>
		<input type='hidden' name = 'subject' value = '".$subject."'>
		
	";
	
}
?>


	<div class='form-group'>
			<label for='ref' class='col-md-3 control-label'>客户要求及出处：</label>
			<div class='col-md-9'>
				<textarea class='form-control' rows='2' name='ref'></textarea>
			</div>
	</div>
	<div class='form-group'>
			<label for='des' class='col-md-3 control-label'>偏离描述：</label>
			<div class='col-md-9'>
				<textarea class='form-control' rows='2' name='des'></textarea>
			</div>
	</div>

	<br />
	<div class='form-group'>
		<div class='col-md-offset-3 col-md-9'>
			<input class='btn btn-success' type = 'submit' value='提交'/>
		</div>
	</div>
</form>
<div id="notes">
<p>操作信息栏</p>
</div>
<br />
<br />
<p class="text-center lead">本专业偏离列表：</p>

<?php
//引用必要的函数
require_once ('../FNS/db_fns.php');


//连接数据库并搜索数据
$db= db_connect();

//把本专业该项目的澄清搜出来
//还需要获取提交人
$query="select deviation.id, deviation.ref, deviation.des, deviation.status, users.name as issue_name from deviation inner join users on deviation.user_id = users.id where deviation.project_id = '".$project_id."' and deviation.subject = '".$subject."' order by deviation.status";
$result=$db->query ($query);
if (!$result) {
	echo "</div>";
	echo "</body>";
	echo "</html>";
	exit;
}
//获取记录数量
$num_results=$result->num_rows;

//写表头
echo "<table class='table table-striped'>";
echo "<tr>";
	echo "<td align='center' class='info'><strong>客户要求</strong></td>";
	echo "<td align='center' class='info'><strong>澄清描述</strong></td>";
	echo "<td align='center' class='info'><strong>提出人</strong></td>";
	echo "<td align='center' class='info'><strong>澄清状态</strong></td>";
	echo "<td align='center' class='info'><strong>操作</strong></td>";
echo "</tr>";



//循环编制表格
for ($i=0; $i < $num_results; $i++) {

	$row=$result->fetch_assoc ();
	
	$ref = stripslashes ($row['ref']);
	$des = stripslashes ($row['des']);
	$issue_name = stripslashes ($row['issue_name']);
	$status = stripslashes ($row['status']);
	$deviation_id = stripslashes ($row['id']);
	$link = "../PHP/edit_deviation.php?deviation_id=".$deviation_id;
	
	
	echo "<tr>";
    echo "<td align ='center'>".$ref."</td>";
	echo "<td align ='center'>".$des."</td>";
	echo "<td align ='center'>".$issue_name."</td>";
	echo "<td align ='center'>".$status."</td>";
	echo "<td align='center'>";
	if ($status == '提交' or $status == '修改') {
		echo "<button class='btn btn-danger btn-sm deletedata' id='".$deviation_id."'>删除</button> ";
	}
	if ($status == '修改') {
		echo "<a class='btn btn-warning btn-sm' href='".$link."' role = 'button'>修改</a> ";
	}
	echo "</td>";
	echo "</tr>";
	
}
echo "</table>";
$result->free();
$db->close();

?>

</div>
</body>
</html>
